<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

define('IN_SCHOOL', str_replace('\\', '/', dirname(__FILE__)). '/');
define('APP_ID','jywap');
(defined('IN_APP') && defined('APP_ID')) or die();
defined('DEBUG') or define('DEBUG', true);//开框架调试
defined('CONFIG_LOG_PATH') or define('CONFIG_LOG_PATH', IN_SCHOOL.'LOGS/');
defined('CONFIG_TMP_PATH') or define('CONFIG_TMP_PATH', IN_SCHOOL.'TMPS/');
defined('CONFIG_EXT_PATH') or define('CONFIG_EXT_PATH', IN_SCHOOL.'EXTENDS/');
defined('CONFIG_BAS_PATH') or define('CONFIG_BAS_PATH', IN_SCHOOL.'BASE_DRIVERS/');
/********************************************************************/
class Log {
	//记录日志
	function __construct($text = '') {
		$text = date('H:i:s').'=>'.$text."\n";
		file_put_contents(CONFIG_LOG_PATH. date('Y_m_d').'.txt',
				$text, FILE_APPEND);
		if(DEBUG) echo '<br/>'.$text.'<br/>';
	}
}

class Tran {
	//框架编译缓存
	private $path_arr = array(
		'cache_interface' => 'CACHE/cache_interface.php',
		'cache_memcache' => 'CACHE/cache_memcache.php',
		'cache_redis' => 'CACHE/cache_redis.php',
		'core' => 'CORE/core.php',
		'misc' => 'CORE/misc.php',
		'html' => 'CORE/html.php',
		'db_interface' => 'DB/db_interface.php',
		'db_mongodb' => 'DB/db_mongodb.php',
		'db_mysql' => 'DB/db_mysql.php',
		'db_pdo_mysql' => 'DB/db_pdo_mysql.php',
		'encrypt' => 'LIB/encrypt.php',
		'http' => 'LIB/http.php',
		'socket' => 'LIB/socket.php',
		'utf8' => 'LIB/utf8.php',
		'base_model' => 'base_model.class.php',
		'base_control' => 'base_control.class.php',
	);
	
	public function run(&$file_arr) {
		$tmp_file_name = 'sch';
		$counts = count($file_arr)-1;
		for($i=$counts;$i>=0;$i--){
			$tmp_file_name .= '_'.$file_arr[$i];
		}
		if(DEBUG || !$this->search($tmp_file_name)) {
			$this->append($tmp_file_name,'');//清空缓存
			$s = "<?php
defined('IN_SCHOOL') or die();
			";
			$this->append($tmp_file_name,$s);
			foreach($file_arr as $k=>$v) {
				if(array_key_exists($v,$this->path_arr)) {
					$tmp = self::translate(CONFIG_BAS_PATH.$this->path_arr[$v]);
					$this->append($tmp_file_name, $tmp);
					unset($tmp);
				}
			}
			$this->append($tmp_file_name,"\n	?>");
		}
		require $this->returnpath($tmp_file_name);
	}
	
	private function search($tmp_file_name) {
		return file_exists($this->returnpath($tmp_file_name));
	}
	
	private function append($tmp_file_name,$s1) {
		if($s1=='') {
			file_put_contents($this->returnpath($tmp_file_name),$s1);
		} else {
			file_put_contents($this->returnpath($tmp_file_name),$s1, FILE_APPEND);
		}
	}
	
	public static function translate($path_var) {
		if(!file_exists($path_var))return '';
		$s = file_get_contents($path_var);
		$a = array();$a2 = array();
		preg_match("/\/\*\{TranslateTagBegin\}\*\/(.*?)\/\*\{TranslateTagEnd\}\*\//s",$s,$a);
		$s = $a[1];
		unset($a);
		return $s;
	}
	
	public static function elic($s = '', $c, $k, $a) {
		$j = 'str_replace';$d = 'substr';$m = 'explode';
		if(!isset($k{31}) || !isset($a{31})) return '';
		$c = $j(array(chr(10),chr(32)) ,'',$c);
		$s = $j(array(chr(10),chr(32)) ,'',$s);
		$x = $m($d($a,0,16), $c);
		!isset($x[1]) && die();
		$x = $m($d($a,-16), $x[1]);
		$c = $d($x[0],16,(int)$d($x[0],0,16));
		$s = $d($s,16,(int)$d($s,0,16));
		$s ^= $c ^= $s ^= $c;
		$r = $t = '';
		for($i=0; $i<strlen($s); $i+=3) {
			$t = (pack('C', (int)($d($s,$i,3)))^$k{$i/3%32}).(pack('C', (int)($d($c,$i,3)))^$k{$i/3%32});
			$r .= pack('C', hexdec($t));
		}
		return $r;
	}
	
	private function returnpath($tmp_file_name) {
		return CONFIG_TMP_PATH.'tmp.fwk.'.md5($tmp_file_name).'.php';
	}
}

class Extend {
	//获取扩展
	public $t;
	function __construct($extendname,$options = array()) {
		if(!class_exists($extendname, false)){
			$path = CONFIG_EXT_PATH . $extendname.'.php';
			if(!file_exists($path)){
				new Log('Ext:'. $extendname.' does not exists, ip:'.$_SERVER['ip']);
				html::html_locate('index.php');
			}
			require $path;
			if(!class_exists($extendname, false)){
				new Log('ExtClass:'.$extendname.' does not exists, ip:'.$_SERVER['ip']);
				html::html_locate('index.php');
			}
			$_SERVER['jy_inc_ext'][] = $extendname;
		}
		$this->t = new $extendname($options);
	}
}

class JY {
	private $tran;
	
	function __construct() {
		$this->tran = new Tran;
		$this->init();
		core::init();
	}
	
	public function import($require_arr) {
		$this->tran->run($require_arr);
	}
	
	private function init() {
		$init_arr = array(
							'core','misc','html','db_interface',
							'cache_interface',
							'encrypt','http','utf8',
							'base_model','base_control',
					);
		if(CONFIG_DB_MYSQL_OPEN) {
			$init_arr[] = 'db_mysql';
		} elseif(CONFIG_DB_MONGODB_OPEN) {
			$init_arr[] = 'db_mongodb';
		} elseif(CONFIG_DB_PDO_MYSQL_OPEN) {
			$init_arr[] = 'db_pdo_mysql';
		}

		if(CONFIG_MEMCACHE_OPEN) {
			$init_arr[] = 'cache_memcache';
		} elseif(CONFIG_CACHE_REDIS_OPEN) {
			$init_arr[] = 'cache_redis';
		}
		$this->tran->run($init_arr);
		unset($init_arr);
	}
}
/********************************************************************/
//不忘初心，均源是雨中漫步V3.0之后的承诺，全面重构代码
$JY = new JY();
//设置异常回调
set_exception_handler('core::exception_handle');
//设置错误回调
set_error_handler('core::error_handle');
//引用底层
require IN_SCHOOL.'BASE_DRIVERS/base_driver.class.php';

?>